package racing.server;

import java.io.IOException;

import racing.client.Message;

import com.lloseng.ocsf.server.AbstractServer;
import com.lloseng.ocsf.server.ConnectionToClient;

/**
 * GameServer for playing multiplayer mode.
 * 
 * @author Sarathit Sangtaweep 5510546182
 * @author Veerapat Teeravipark 5510547022
 * @version 2013
 */
public class Server extends AbstractServer {
	private static int countClient = 0;
	private boolean hasRoom = false;
	private int countDice = 1;
	private int start = 0;
	private Message dice;
	private int id = 0;
	private boolean case1 = false;

	/**
	 * Setup port server.
	 * @param port port of server.
	 */
	public Server(int port) {
		super(port);
	}

	@Override
	protected void clientConnected(ConnectionToClient client) {
		countClient++;
		if (countClient < 3) {
			System.out.println("connected : " + countClient);
			try {
				client.sendToClient(new Message("id", countClient));
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			try {
				client.sendToClient(new Message("Server Full"));
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected synchronized void clientDisconnected(ConnectionToClient client) {
		countClient--;
		if (countClient == 1) {
			sendToAllClients(new Message("Other person disconnected"));
		}
		if (countClient == 0) {
			hasRoom = false;
			start = 0;
			countDice = 1;
		}
		if (case1) {
			countClient--;
			case1 = false;
		}
		System.out.println("disconnected : " + countClient);
	}

	@Override
	protected void handleMessageFromClient(Object msg, ConnectionToClient client) {
		if (countClient > 0 && countClient < 3) {
			if (msg instanceof Message) {
				Message message = (Message) msg;
				String messageText = message.getCommand();
				if (messageText.equals("setid")) {
					id++;
					System.out.println("set id");
					try {
						client.sendToClient(new Message("id", id));
					} catch (IOException e) {
						e.printStackTrace();
					}

				}
				else if (messageText.equals("create room")) {
					System.out.println("create room");
					if (hasRoom) {
						try {
							System.out.println("has room");
							client.sendToClient(new Message("hasRoom"));
							client.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
						countClient++;
						case1 = true;
					} else {
						hasRoom = true;
					}
				} 
				else if (messageText.equals("join room") && !hasRoom) {
					try {
						System.out.println("no room");
						client.sendToClient(new Message("noRoom"));
						client.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				else if (messageText.equals("sendDice")) {
					if (countDice == 1) {
						dice = message;
					}
					if (countDice == 2) {
						sendToAllClients(dice);
						sendToAllClients(message);
						System.out.println("confirm");
						sendToAllClients(new Message("confirmRoll"));
						countDice = 0;
					}
					countDice++;
				}
				else if (messageText.equals("sendRPS")) {
					if (countDice == 1) {
						dice = message;
					}
					if (countDice == 2) {
						sendToAllClients(dice);
						sendToAllClients(message);
						System.out.println("confirm");
						sendToAllClients(new Message("confirmRPS"));
						countDice = 0;
					}
					countDice++;
				}
				else if (messageText.equals("startAll")) {
					start++;
					System.out.println("StartAll1");
					if (start == 2) {
						System.out.println("StartAll2");
						sendToAllClients(new Message("startAll"));
					}
				} 
				else if (countClient == 2) {
					System.out.println("System : " + messageText);
					sendToAllClients(message);
				} 
				else {
					try {
						client.sendToClient(new Message("wait"));
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
